namespace CISLib
{
    using System;
    using System.Collections;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.CompilerServices;

    public class ManageMail
    {
        [MethodImpl(MethodImplOptions.Synchronized)]
        public static EmailPool[] GetEmailsViaFlag(int flag, int mailSendCountPerJob)
        {
            SqlDataReader MyReader = null;
            ArrayList mailList = new ArrayList();
            DBAccess MyDB = new DBAccess();
            MyDB.DBOpen();
            MyDB.AddParameters(flag, "EmailFlag", SqlDbType.Int);
            string strSQL = "Select * from TB_EmailPool where EmailFlag = @EmailFlag";
            MyDB.DBSetSelect(strSQL);
            MyDB.DBSetSelect(strSQL);
            MyDB.DBExcuteReader();
            MyReader = MyDB.DBReader;
            while (MyReader.Read())
            {
                EmailPool mail = new EmailPool();
                mail.EmailId = MyReader.GetGuid(0);
                mail.Sender = MyReader.GetString(1);
                mail.Recipient = MyReader.GetString(2);
                mail.Cc = "";
                mail.Bcc = "";
                mail.Subject = MyReader.GetString(5);
                mail.Body = MyReader.GetString(6);
                mail.BodyType = MyReader.GetInt32(7);
                mail.CrtTime = MyReader.GetDateTime(8);
                mail.EmailFlag = MyReader.GetInt32(9);
                mailList.Add(mail);
            }
            MyDB.DBClose();
            return (EmailPool[]) mailList.ToArray(typeof(EmailPool));
        }

        public static void InsertNewMail(string sender, string recipient, string subject, DateTime crtTime, int bodyType, string body)
        {
            Guid emailId = Guid.NewGuid();
            DBAccess MyDB = new DBAccess();
            MyDB.DBOpen();
            MyDB.AddParameters(emailId, "EmailId", SqlDbType.UniqueIdentifier);
            MyDB.AddParameters(sender, "Sender", SqlDbType.VarChar);
            MyDB.AddParameters(recipient, "Recipient", SqlDbType.VarChar);
            MyDB.AddParameters(subject, "Subject", SqlDbType.VarChar);
            MyDB.AddParameters(crtTime, "CrtTime", SqlDbType.DateTime);
            MyDB.AddParameters(bodyType, "BodyType", SqlDbType.Int);
            MyDB.AddParameters(body, "Body", SqlDbType.Text);
            MyDB.AddParameters(0, "EmailFlag", SqlDbType.Int);
            string strSQL = "Insert into TB_EmailPool (EmailId,Sender,Recipient,Subject,CrtTime,BodyType,Body,EmailFlag) values (@EmailId,@Sender,@Recipient,@Subject,@CrtTime,@BodyType,@Body,@EmailFlag)";
            MyDB.DBSetSelect(strSQL);
            MyDB.DBExcuteNonQuery();
            MyDB.DBClose();
        }

        public static void RemoveEmailViaId(Guid emailId)
        {
            DBAccess MyDB = new DBAccess();
            MyDB.DBOpen();
            MyDB.AddParameters(emailId, "EmailId", SqlDbType.UniqueIdentifier);
            string strSQL = "Delete From TB_EmailPool where EmailId = @EmailId";
            MyDB.DBSetSelect(strSQL);
            MyDB.DBExcuteNonQuery();
            MyDB.DBClose();
        }
    }
}

